package top.leoseven.adminproject.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import top.leoseven.adminproject.R;
import top.leoseven.adminproject.modle.HomeWork;
import top.leoseven.adminproject.modle.KeCheng;
import top.leoseven.adminproject.modle.Student;
import top.leoseven.adminproject.view.MainActivity;
import top.leoseven.adminproject.view.StuHomeWorkList;

/**
 * Created by admin2 on 2018/4/11.
 */

public class MainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int STU=1;
    private static final int HOMEWORK=2;
    private static final int KECHENG=3;
    private List objects=new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;

    public MainAdapter(List objects, Context context) {
        this.objects = objects;
        this.context = context;
        this.inflater=LayoutInflater.from(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==STU) {
            View v = inflater.inflate(R.layout.item_student, parent, false);
            return new StuHolder(v);
        }else if (viewType==KECHENG){
            View view = inflater.inflate(R.layout.item_kecheng, parent, false);
            return new KechengHolder(view);
        }else if (viewType==HOMEWORK){
            View v = inflater.inflate(R.layout.item_homework, parent, false);
            return new HomeWorkHolder(v);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof StuHolder) {
            final Student stu= (Student) objects.get(position);
            ((StuHolder) holder).stuname.setText(""+stu.getStuname());
            ((StuHolder) holder).stunum.setText(""+stu.getId());
            ((StuHolder) holder).bjname.setText(""+stu.getBjname());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent it = new Intent(context, StuHomeWorkList.class);
                    it.putExtra("type",StuHomeWorkList.selectByStuID);
                    it.putExtra("stuid",stu.getId());
                    context.startActivity(it);
                }
            });
        }else if (holder instanceof KechengHolder){
            final KeCheng c = (KeCheng) objects.get(position);
            ((KechengHolder) holder).kcname.setText(c.getName());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent it = new Intent(context, StuHomeWorkList.class);
                    it.putExtra("type",StuHomeWorkList.selectByKechengID);
                    it.putExtra("kcid",c.getId());
                    context.startActivity(it);
                }
            });
        }else if (holder instanceof HomeWorkHolder){
            final HomeWork h= (HomeWork) objects.get(position);
            String date = new java.text.SimpleDateFormat("yyyy-MM-DD").format(new java.util.Date(h.getTime()));
            ((HomeWorkHolder) holder).worktime.setText(date);
            ((HomeWorkHolder) holder).workname.setText(h.getName());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent it = new Intent(context, StuHomeWorkList.class);
                    it.putExtra("type",StuHomeWorkList.selectByHomeWorkID);
                    it.putExtra("workid",h.getId());
                    context.startActivity(it);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return objects.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (objects.get(position) instanceof Student) {
            return STU;
        }else if (objects.get(position) instanceof HomeWork){
            return HOMEWORK;
        }else if (objects.get(position) instanceof KeCheng){
            return KECHENG;
        }
        return super.getItemViewType(position);
    }

    class StuHolder extends RecyclerView.ViewHolder{
        private TextView stuname,stunum,bjname;
        public StuHolder(View itemView) {
            super(itemView);
            stuname= (TextView) itemView.findViewById(R.id.stuname);
            stunum= (TextView) itemView.findViewById(R.id.stunum);
            bjname= (TextView) itemView.findViewById(R.id.bjname);
        }
    }

    class KechengHolder extends RecyclerView.ViewHolder{
        private TextView kcname;
        public KechengHolder(View itemView) {
            super(itemView);
            kcname= (TextView) itemView.findViewById(R.id.kcname);
        }
    }

    class HomeWorkHolder extends RecyclerView.ViewHolder{
        private TextView workname,worktime;
        public HomeWorkHolder(View itemView) {
            super(itemView);
            workname= (TextView) itemView.findViewById(R.id.workname);
            worktime= (TextView) itemView.findViewById(R.id.time);
        }
    }
}
